package com.gitee.ice1938.adcode;

import javax.sql.DataSource;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cache.CacheManager;
import org.springframework.cache.MissingJCacheCacheManager;
import org.springframework.cache.annotation.Caching;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.jcache.JCacheCacheManager;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;

import liquibase.integration.spring.SpringLiquibase;

/**
 */
@Configuration("cocklebur_dict_administrative_area_configuration")
@EnableCaching
@ComponentScan("com.gitee.ice1938.adcode")
public class AdminDictAdministrativeConfig implements InitializingBean {

	public static final String cach_cityselect = "cach_cityselect";

	@Bean // LiquibaseAutoConfiguration
	public SpringLiquibase liquibase(DataSource dataSource) {
		SpringLiquibase liquibase = new SpringLiquibase();
		liquibase.setDataSource(dataSource);
		// 指定changelog的位置，这里使用的一个master文件引用其他文件的方式
		// <includeAll path="classpath*:db/changelog" relativeToChangelogFile="false"/>
		// 这里有个问题就是被引用的jar 都会初始话,不能根据需要进行是否启用
		liquibase.setChangeLog("classpath:/db/main.changelog-master.xml");
		// liquibase.setContexts("development,test,production");
		liquibase.setShouldRun(true);
		return liquibase;
	}

	@Autowired
	private MessageSource messageSource;

	@Override
	public void afterPropertiesSet() throws Exception {
		// 根据模块信息添加i18n 默认配置 MessageSourceAutoConfiguration会根据是否配置了前缀进行资源相关类加载
		ResourceBundleMessageSource res = (ResourceBundleMessageSource) messageSource;
		res.addBasenames("i18n/admin_dict_administrative");
	}

	@Bean
	@ConditionalOnClass({ Caching.class, JCacheCacheManager.class })
	@ConditionalOnMissingBean(org.springframework.cache.CacheManager.class)
	public CacheManager cacheManager() {
		return new MissingJCacheCacheManager();
	}
}
